package com.smfx.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.daoSqlserver.StudentMonthPointsMapper;
import com.smfx.yxt.daoSqlserver.StudentTermPointsMapper;
import com.smfx.yxt.model.StudentMonthPoints;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.PointsReqVO;
import com.smfx.yxt.service.IStudentPointService;
import com.smfx.yxt.view.PointItemView;
import com.smfx.yxt.view.PointTotalView;

@Service("studentPointService")
public class StudentPointServiceImpl implements IStudentPointService {
	
	@Autowired
	private StudentTermPointsMapper studentTermPointsMapper;
	@Autowired
	private StudentMonthPointsMapper studentMonthPointsMapper;
	
	@Override
	public int addStudentMonthPoints(PointsReqVO vo) {
		return studentMonthPointsMapper.addStudentMonthPoints(vo);
	}

	@Override
	public int addStudentTermPoints(PointsReqVO vo) {
		return studentTermPointsMapper.addStudentTermPoints(vo);
	}

	@Override
	public int updateStudentTermPoints(PointsReqVO vo) {
		return studentTermPointsMapper.updateStudentTermPoints(vo);
	}

	@Override
	public int updateStudentTotalPoints(PointsReqVO vo) {
		return studentTermPointsMapper.updateStudentTotalPoints(vo);
	}

	@Override
	public int updateStudentMonthPoints(Integer studentId, Float point) {
		int result = startUpdatePoints(studentId, point);
		if(result == 0) {
			for(int i = 0; i < 2; i++) {
				result = startUpdatePoints(studentId, point);
				if(result == 1) {
					break;
				}
			}
		}
		return result;
	}

	private int startUpdatePoints(Integer studentId, Float point) {
		PointsReqVO vo = new PointsReqVO();
		vo.setKeyId(studentId);
		StudentMonthPoints student = studentMonthPointsMapper.selectStudentForCurrMonth(vo);
		Float points = student.getPoints() == null ? 0 : student.getPoints();
		student.setPoints(points + point);
		return studentMonthPointsMapper.updateForVersionById(student);
	}

	@Override
	public PointTotalView selectTotalPointForStudent(PointsReqVO vo) {
		return studentTermPointsMapper.selectTotalPointForStudent(vo);
	}

	@Override
	public List<PointTotalView> selectTotalPointForStudentByPage(Page<PointTotalView> page) {
		return studentTermPointsMapper.selectTotalPointForStudentByPage(page);
	}

	@Override
	public List<PointItemView> selectPointItemForStudentByPage(Page<PointItemView> page) {
		return studentTermPointsMapper.selectPointItemForStudentByPage(page);
	}

}
